package com.itheima.pattern.composition;

import java.util.ArrayList;
import java.util.List;

//菜单类：属于树枝角色
public class Menu extends MenuComponent{

    //存储子菜单和子菜单项
    private List<MenuComponent> menuComponentsList=new ArrayList<>();

    public Menu(String name,int level){
        this.name=name;
        this.level=level;
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponentsList.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponentsList.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponentsList.get(index);
    }

    @Override
    public void print() {

        for (int i = 1; i < level; i++) {
            System.out.print("--");
        }
        //先打印菜单名称
        System.out.println(this.name);

        //打印子菜单或者子菜单项名称。
        for(MenuComponent component:menuComponentsList){

            component.print();
        }

    }
}
